import cv2
import numpy as np
import pyautogui


def capture_screen(x, y, width, height):
    """
    捕获指定区域的屏幕截图
    :param x: 区域左上角 x 坐标
    :param y: 区域左上角 y 坐标
    :param width: 区域宽度
    :param height: 区域高度
    :return: 转换为 OpenCV 格式的截图
    """
    screenshot = pyautogui.screenshot(region=(x, y, width, height))
    return cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
if  __name__ == '__main__':
    #循环获取屏幕截图
    while True:
        # 截图区域   x, y, width, height
        x = 0
        y = 87
        width = 842
        height = 570

        # 捕获屏幕截图
        screenshot = capture_screen(x, y, width, height)

        # 显示截图
        cv2.imshow('screenshot', screenshot)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
